package com.zhuhjay.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuhjay.model.entity.SystemLog;
import com.zhuhjay.service.SystemLogService;
import com.zhuhjay.utils.result.Result;
import com.zhuhjay.utils.result.ResultBean;
import com.zhuhjay.utils.result.ResultMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.zhuhjay.enums.HttpCode.DELETE_SUCCESS;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhuhjay
 * @since 2023-01-30
 */
@Api(tags = "系统日志控制器")
@RestController
@RequestMapping("/log")
public class SystemLogController {

    @Resource
    private SystemLogService logService;

    @ApiOperation("所有的日志")
    @GetMapping("list")
    public Result list() {
        return ResultBean.ok().data(logService.list());
    }

    @ApiOperation("分页查询日志")
    @GetMapping("page/{pageNum}/{pageSize}")
    public Result page(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        Page<SystemLog> page = Page.of(pageNum, pageSize);
        return ResultMap.ok().beanToData(logService.page(page),
                "total", "current", "size", "records");
    }

    @ApiOperation("删除日志")
    @DeleteMapping("{id}")
    public Result deleteOne(@PathVariable Integer id) {
        logService.removeById(id);
        return ResultBean.ok(DELETE_SUCCESS);
    }

    @ApiOperation("批量删除日志")
    @DeleteMapping()
    public Result deleteBatch(@RequestParam List<Integer> ids) {
        if (ids.isEmpty()) {
            return ResultMap.fail("请选择要删除的日志");
        }
        logService.removeByIds(ids);
        return ResultBean.ok(DELETE_SUCCESS);
    }

}
